<?php
namespace Components\User;

class Di extends \Phalcon\Di {
  protected function setFactory($name = null, $parameters = null, $is_shared = true) {
    if(is_null($name)) {
      $name = $this -> getShared('config') -> get('adapter');
    }
    //$name = ucfirst(strtolower($name));

    if(!$this -> getDI() -> has($name)) {
      $this -> getDI() -> set($name, function() use ($name) {
        $className = __NAMESPACE__ . '\\' . $name . '\\Factory';
        if (!class_exists($className)) {
          throw new \Exception($className . ' is non-existent.');
        }

        $config = $this -> getShared('config');
        return !$config -> get($name)?new $className([]):new $className($config -> get($name) -> toArray());
      });
    }

    return $is_shared?$this -> getDI() -> getShared($name):$this -> getDI() -> get($name);
  }

  use \Components\Traits\DiTraits;
}
